#include "stdafx.h"
#include <ScriptingModules/LuaScriptModule/CEGUILua.h>
#include <conio.h>
#include <RendererModules/Ogre/CEGUIOgreRenderer.h>

#include "PapaBall.h"
//#include "PapaBallLuaScript.h"
#include "PapaBallSceneManager.h"
#include "utility.h"
//-------------------------------------------------------------------------------------
PapaBallMain::PapaBallMain(void)
{
	
}
//-------------------------------------------------------------------------------------
PapaBallMain::~PapaBallMain(void)
{
}

//-------------------------------------------------------------------------------------
int  tolua_PapaBall_open (lua_State* tolua_S);

void PapaBallMain::InitialKinect()
{
	PRINT("Kinect Initialization Start");
	Kinect::getSingleton().initPrimeSensor();
	PRINT("Kinect Installed");
}

void PapaBallMain::InitialLuaModel()
{
	
	PRINT("CEGUI:LuaModule:Initialization");
	CEGUI::LuaScriptModule &LuaScript= CEGUI::LuaScriptModule::create();
	//PapaBall::PapaBallLuaScriptModule &LuaScript = PapaBall::PapaBallLuaScriptModule::create();
	tolua_PapaBall_open(LuaScript.getLuaState());
	CEGUI::OgreRenderer &render = CEGUI::OgreRenderer::bootstrapSystem();
	CEGUI::System::getSingleton().setScriptingModule(&LuaScript);
	CEGUI::ScriptModule::setDefaultResourceGroup("Script");
	Runscript("test.lua");
	PRINT("CEGUI:LuaModule:Installed");

}

bool PapaBallMain::setup()
{
	bool ret = BaseApplication::setup();
	InitialKinect();
	InitialCEGUI();
	InitialLuaModel();
	MenuManager::getSingleton().Initialization();
	PapaBallSceneManager::getSingleton().setup();

	return ret;
}

bool PapaBallMain::configure()
{
	bool ret = BaseApplication::configure();
	PRINT("SceneManager:Setup Window");
	PapaBallSceneManager::getSingleton().setupWindow();
	return ret;
}

void PapaBallMain::InitialCEGUI(void)
{
	PRINT("CEGUI:Initialization Start");
	CEGUI::Imageset::setDefaultResourceGroup("Imagesets");
	CEGUI::Font::setDefaultResourceGroup("Fonts");
	CEGUI::Scheme::setDefaultResourceGroup("Schemes");
	CEGUI::WidgetLookManager::setDefaultResourceGroup("LookNFeel");
	CEGUI::WindowManager::setDefaultResourceGroup("Layouts");
	PRINT("CEGUI:Initialization End");
}



#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
    int main(int argc, char *argv[])
#endif
    {
        // Create application object
        PapaBallMain app;
#ifdef _DEBUG
		AllocConsole();
		freopen("CONOUT$","w",stdout);
#endif
        try {
			
            app.go();

        } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
            MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
            std::cerr << "An exception has occured: " <<
                e.getFullDescription().c_str() << std::endl;
#endif
        }
#ifdef _DEBUG
		FreeConsole();
#endif
        return 0;
    }

#ifdef __cplusplus
}
#endif
